package Model
{
	import SharedCloud.SyncedObject;
	
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.events.CollectionEvent;
	import mx.events.CollectionEventKind;
	
	[Event(name="DeleteBacklog",type="flash.events.Event")]
	
	[Bindable]
	public class Backlog extends SyncedObject
	{
		public var itemName:String;
		public var list:ArrayCollection = new ArrayCollection();
		
		public function Backlog()
		{
			super();
			list.addEventListener(CollectionEvent.COLLECTION_CHANGE, listChanged);
		}
		
		protected function listChanged(evt:CollectionEvent):void
		{
			for(var i:int=0; i<evt.items.length; i++)
			{
				if(evt.kind == CollectionEventKind.ADD)
					IEventDispatcher(evt.items[i]).addEventListener("DeleteStory", deleteStory);
				if(evt.kind == CollectionEventKind.REMOVE)
					IEventDispatcher(evt.items[i]).removeEventListener("DeleteStory", deleteStory);
			}
		}
		protected function deleteStory(evt:Event):void
		{
			list.removeItemAt(list.getItemIndex(evt.target));
		}
		
		public function remove():void
		{
			IEventDispatcher(this).dispatchEvent(new Event("DeleteBacklog"));
		}
	}
}